package com.major.crowd.mvc.controller;

import com.major.crowd.entity.Menu;
import com.major.crowd.service.api.MenuService;
import com.major.crowd.util.ResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Controller
public class MenuController {
    @Autowired
    private MenuService menuService;

    @ResponseBody
    @RequestMapping("menu/remove.json")
    public ResultEntity<String> removeMenu(Integer id){
        menuService.removeMenu(id);
        return ResultEntity.successWithoutData();
    }

    @ResponseBody
    @RequestMapping("menu/update.json")
    public ResultEntity<String> updateMenu(Menu menu){
        menuService.updateMenu(menu);
        return ResultEntity.successWithoutData();
    }

    @ResponseBody
    @RequestMapping("menu/save.json")
    public ResultEntity<String> saveMenu(Menu menu) throws InterruptedException {
        menuService.saveMenu(menu);
        Thread.sleep(2000);
        return ResultEntity.successWithoutData();

    }

    @ResponseBody
    @RequestMapping("menu/get/whole/tree.json")
    public ResultEntity<Menu> getWholeTree(){
        //查询全部menu对象
        List<Menu> menuList = menuService.queryAllMenu();
        //声明一个变量存储找到的根节点
        Menu root = null;
        //创建Map，存储id和menu对象，便于查找父节点
        Map<Integer,Menu> menuMap = new HashMap<>();
        //填充menuMap
        for (Menu menu : menuList) {
            Integer id = menu.getId();
            menuMap.put(id,menu);
        }
        //查找跟节点，组装父子结点
        for (Menu menu : menuList) {
            //先获取父节点
            Integer pid = menu.getPid();
            //判断是否为根节点
            if (pid == null){
                root = menu;
                continue;
            }
            //不是根节点，找到他的父节点
            Menu father = menuMap.get(pid);
            father.getChildren().add(menu);
        }
        return ResultEntity.successWithData(root);
    }



    public ResultEntity<Menu> getWholeTreeOld(){
        //查询出全部的menu对象
        List<Menu> menus = menuService.queryAllMenu();
        //声明一个变量来存储找到的根节点
        Menu root = null;
        //遍历menus
        for (Menu menu : menus) {
            //获取pid
            Integer pid = menu.getPid();
            if (pid == null){
                //赋给root
                root = menu;
                //停止本次循环，执行下一次循环
                continue;
            }
            //不为null，有父节点，建立父子关系
            for (Menu maybeFather :menus) {
                Integer id = maybeFather.getId();
                //将子节点pid与maybeFather.id比较
                if (Objects.equals(pid,id)){
                    //将子节点存入父节点的children集合
                    maybeFather.getChildren().add(menu);

                    break;
                }

            }
        }
        return ResultEntity.successWithData(root);
    }
}
